import 'dart:io';

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_syt/application.dart';
import 'package:flutter_syt/config/app_color.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_syt/domain/widgets/common_dialog.dart';
import 'package:flutter_syt/domain/widgets/custom_dialog.dart';
import 'package:flutter_syt/generated/assets.dart';
import 'package:flutter_syt/generated/l10n.dart';
import 'package:flutter_syt/global_common.dart';
import 'package:flutter_syt/theme/theme_color.dart';
import 'package:flutter_syt/widgets/auto_image.dart';
import 'package:flutter_syt/widgets/web_protocol.dart';

// getTimeStamp
class Splash extends StatefulWidget {
  final Duration duration;

  // final Widget next;
  final Function getMainView;

  Splash(
      {this.duration = const Duration(seconds: 3), required this.getMainView});

  @override
  State<StatefulWidget> createState() {
    return _SplashState();
  }
}

class _SplashState extends State<Splash> {
  bool timerDone = false;
  bool isAgree = false;

  buildMainPage() async {
    if (timerDone && isAgree) {
      // appContext
      await Application.lazyRegister();

      Widget mainWidget = await widget.getMainView();
      Navigator.of(context).pushReplacement(
        PageRouteBuilder(pageBuilder: (ctx, a, second) {
          //  Widget mainWidget = await widget.getMainView();
          return FadeTransition(opacity: a, child: mainWidget);
        }),
      );
    }
  }

  checkIsShowAgree() {
    if (!appContext.storage.getBool('isAgree')) {
      Future.delayed(Duration.zero, () {
        showCommonDialog(
          context,
          DialogEntity(
              S.of(context).guide_note_title,
              Container(
                constraints: BoxConstraints(maxHeight: 350),
                child: ListView.builder(
                  shrinkWrap: true,
                  itemCount: 1,
                  physics: ClampingScrollPhysics(),
                  itemBuilder: (context, index) {
                    return RichText(
                      text: TextSpan(
                        children: [
                          WidgetSpan(
                              child: SizedBox(
                            height: 5,
                          )),
                          TextSpan(
                            text: S.of(context).guide_note_content1,
                            style: TextStyle(
                              color: ThemeColor.colorBlack100,
                              fontSize: 16,
                              fontWeight: FontWeight.w300,
                            ),
                          ),
                          WidgetSpan(
                              child: SizedBox(
                            height: 5,
                          )),
                          TextSpan(
                            text: S.of(context).guide_note_privacy,
                            style: TextStyle(
                              color: ThemeColor.linkColor,
                              fontSize: 16,
                              fontWeight: FontWeight.w300,
                            ),
                            recognizer: TapGestureRecognizer()
                              ..onTap = () {
                                Navigator.of(context).push(MaterialPageRoute(
                                    builder: (ctx) => WebProtocolDetailPage(
                                        GlobalData.privacyUrl(context))));
                              },
                          ),
                          TextSpan(
                            text: ' ' + S.of(context).guide_note_and + ' ',
                            style: TextStyle(
                              color: ThemeColor.colorBlack100,
                              fontSize: 16,
                              fontWeight: FontWeight.w300,
                            ),
                          ),
                          TextSpan(
                            text: S.of(context).guide_note_protocol,
                            style: TextStyle(
                              color: ThemeColor.linkColor,
                              fontSize: 16,
                              fontWeight: FontWeight.w300,
                            ),
                            recognizer: TapGestureRecognizer()
                              ..onTap = () {
                                Navigator.of(context).push(MaterialPageRoute(
                                    builder: (ctx) => WebProtocolDetailPage(
                                        GlobalData.userAgreeUrl(context))));
                              },
                          ),
                          TextSpan(
                            text: S.of(context).guide_note_content2,
                            style: TextStyle(
                              color: ThemeColor.colorBlack100,
                              fontSize: 16,
                              fontWeight: FontWeight.w300,
                              fontFamily: 'PingFang SC',
                            ),
                          ),
                          TextSpan(
                            text: S.of(context).guide_note_content3,
                            style: TextStyle(
                              color: ThemeColor.colorBlack100,
                              fontSize: 16,
                              fontWeight: FontWeight.w300,
                              fontFamily: 'PingFang SC',
                            ),
                          ),
                          TextSpan(
                            text: S.of(context).guide_note_privacy,
                            style: TextStyle(
                              color: ThemeColor.linkColor,
                              fontSize: 16,
                              fontWeight: FontWeight.w300,
                            ),
                            recognizer: TapGestureRecognizer()
                              ..onTap = () {
                                Navigator.of(context).push(MaterialPageRoute(
                                    builder: (ctx) => WebProtocolDetailPage(
                                        GlobalData.privacyUrl(context))));
                              },
                          ),
                          TextSpan(
                            text: ' ' + S.of(context).guide_note_and + ' ',
                            style: TextStyle(
                              color: ThemeColor.colorBlack100,
                              fontSize: 16,
                              fontWeight: FontWeight.w300,
                            ),
                          ),
                          TextSpan(
                            text: S.of(context).guide_note_protocol,
                            style: TextStyle(
                              color: ThemeColor.linkColor,
                              fontSize: 16,
                              fontWeight: FontWeight.w300,
                            ),
                            recognizer: TapGestureRecognizer()
                              ..onTap = () {
                                Navigator.of(context).push(MaterialPageRoute(
                                    builder: (ctx) => WebProtocolDetailPage(
                                        GlobalData.userAgreeUrl(context))));
                              },
                          ),
                          TextSpan(
                            text: S.of(context).guide_note_content4,
                            style: TextStyle(
                              color: ThemeColor.colorBlack100,
                              fontSize: 16,
                              fontWeight: FontWeight.w300,
                              fontFamily: 'PingFang SC',
                            ),
                          ),
                        ],
                      ),
                    );
                  },
                ),
              ), () {
            Navigator.of(context).pop();
            showCommonDialog(
              context,
              DialogEntity(S.of(context).guide_note2_title,
                  Text(S.of(context).guide_note2_content), () => exit(0), () {
                appContext.storage.setBool('isAgree', true);
                Navigator.of(context).pop();
                isAgree = true;
                buildMainPage();

                // VersionUpdate.update(context, false);
                // Application.lazyRegister();
              },
                  cancelText: S.of(context).guide_note2_cancel,
                  okText: S.of(context).guide_note2_agree,
                  disableAndriodPop: true),
            );
          }, () {
            appContext.storage.setBool('isAgree', true);
            Navigator.of(context).pop();
            isAgree = true;
            buildMainPage();
          },
              cancelText: S.of(context).guide_note_refused,
              okText: S.of(context).guide_note_agree,
              disableAndriodPop: true),
        );
      });
    } else {
      isAgree = true;
      buildMainPage();
    }
  }

  @override
  void initState() {
    Future.delayed(widget.duration, () async {
      timerDone = true;
      buildMainPage();
      // Widget mainWidget = await widget.getMainView();
      // Navigator.of(context).pushReplacement(
      //   PageRouteBuilder(pageBuilder: (ctx, a, second) {
      //     return FadeTransition(opacity: a, child: mainWidget);
      //   }),
      // );
    });

    checkIsShowAgree();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        width: double.infinity,
        decoration: const BoxDecoration(
            image: DecorationImage(
                image: AssetImage(
                  Assets.splashBg,
                ),
                fit: BoxFit.fitWidth,
                alignment: Alignment.topCenter)),
        child: Column(
          // crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            // Image.asset('assets/videos/launch.gif'),
            Padding(padding: EdgeInsets.only(bottom: 179.w)),
            AutoImage(Assets.launch,width: 212.w, height: 212.w),
            // Image.asset('assets/videos/launch.gif', width: 213, height: 212),
            // Image.asset("assets/icons/open.png",width: 213,height: 212,),
            const Spacer(),
            // Image.asset(
            //   "assets/icons/openBottom.png",
            //   width: 148,
            //   height: 35,
            // ),
            Text(
              '成都世运会',
              style: TextStyle(
                color: ThemeColor.color8993A6,
                fontSize: 14.sp,
              ),
            ),
            54.verticalSpaceFromWidth,
            // Padding(padding: EdgeInsets.only(bottom: 104)),
          ],
        ),
      ),
    );
  }
}

// header: CustomHeader(
//         height: kHeight,
//         builder: (ctx, state) {
//           var txt = S.of(context).loading;
//           if (state == RefreshStatus.failed) {
//             txt = S.of(context).load_failed;
//           } else if (state == RefreshStatus.canRefresh) {
//             txt = S.of(context).release_to_refresh;
//           } else if (state == RefreshStatus.idle) {
//             txt = S.of(context).swipe_to_refresh;
//           } else if (state == RefreshStatus.completed) {
//             txt = S.of(context).refreshed;
//           }
//           Widget headerWidget = commonRefreshStateWidget(txt, Colors.white);
//           if (widget.refreshItemBuilder != null) {
//             headerWidget = widget.refreshItemBuilder!(ctx, txt, state!);
//           }
//           return headerWidget;
//         },
//       ),
//       footer: CustomFooter(
//         builder: (context, state) {
//           var txt = S.of(context).loading;
//           if (state == LoadStatus.failed) {
//             txt = S.of(context).load_failed;
//           } else if (state == LoadStatus.noMore) {
//             txt = S.of(context).no_more;
//           } else if (state == LoadStatus.idle) {
//             txt = S.of(context).load_more;
//           }
//           return Container(
//             child: Column(
//               crossAxisAlignment: CrossAxisAlignment.center,
//               children: [
//                 SizedBox(
//                   height: 20,
//                 ),
//                 Image.asset(
//                   "assets/icons/refresh_footer.png",
//                   width: 36,
//                   height: 32,
//                 ),
//                 Text(
//                   txt,
//                   style: TextStyle(
//                       color: Colors.black.withOpacity(0.4), fontSize: 11),
//                 ),
//               ],
//             ),
//           );
//         },
//       ),
